// pages/coupons/coupons.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showList:[],   //展示的优惠券列表
        id:'',
        token:'',
        typeActive: 1,
        stateActive: 0,
        typeList:[],
        color: '#000',
        coupons: '../../image/picture/couponBg.png',
        typeId:1,
        couponListArray:[],
        typeListArray:[
            {id:0,title:'未使用',titletitle:'立即使用',click:'onClick'},
            {id:1,title:'已使用',titletitle:'已使用',click:''},
            {id:2,title:'已赠送',titletitle:'已赠送',click:''},
            {id:3,title:'已过期',titletitle:'已过期',click:''},
        ]
    },
    onLoad: function () {
        
       
    },
    onTypeActiveChange (e) {
        let index = e.currentTarget.dataset.index
        this.setData({
            showList:[],
            typeActive: index
        })
        this.getCouponById()
    },
    onStateActiveChange (e) {
        let index = e.detail.index
        this.setData({
            stateActive:index
        })
        this.getShowList()
    },
    onClick (e) {
        
        let id = e.currentTarget.id
        wx.navigateTo({ url: '/pages/coupons-details/coupons-details?id='+id })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
            var that = this;
            var that = this;
            var uinfo = wx.getStorageSync('uinfo')
            if(uinfo.user){
                that.setData({
                    id:uinfo.user.id,
                    token:uinfo.token
                })
            }
            that.getCoupoTypenById();
            that.getCouponById();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    // 根据用户id获取优惠卷
    getCouponById(){
        var that = this;
        wx.$api.getCouponListInfo({
              userId:that.data.id,
              couponTypeId:that.data.typeActive+1||'1'
          }).then(res=>{
              if(res.code){
                that.setData({
                    couponListArray:res.data
                })
                that.getShowList()
              }else{
                  wx.showToast({
                    title: res.msg||'请求失败',
                    icon:'none',
                    duration:1000
                  })
              }
        })
    },
    getShowList(){  
        var that = this
        let arr = []
        let all = that.data.couponListArray
        for (let i = 0; i < all.length; i++) {
            const el = all[i];
            if(el.coupon_use_status == that.data.stateActive){
                if(el.stop_time){
                    el.stop_time=el.stop_time.substring(0,10)
                }
                arr.push(el)
            }
        }
        that.setData({
            showList:arr
        })
    },
    toUse(data){
        wx.navigateTo({
          url: '/pages/goods-details/goods-details?couponsId='+data.currentTarget.id,
        })
    },
    //获取用户优惠卷类型
    async getCoupoTypenById(){
        wx.showLoading()
        var that = this;
        await wx.$api.getCouponTypeListInfo({
              userId:that.data.id
          }).then(async res=>{
            if(res.code){
                let arr = res.data
                that.setData({
                    typeList:arr
                })
                wx.hideLoading()
            }else{
                wx.showToast({
                  title: res.msg||'请求失败',
                  icon:'none',
                  duration:1000
                })
            }
          })
    },
    onClickTabs(event) {
        // event.detail.index+'======'+event.detail.title
        var index = event.detail.index;
        var that = this;
        index+=1;
        
      },
      goCar(e){   //去购物
        let index = e.currentTarget.dataset.index
        if(index&&index>0)  return
          wx.switchTab({
            url: '/pages/exclusive/exclusive',
          })
      },
})